package cn.agiledata.bank.scores.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.account.service.AccountSummary;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.PageBean;

import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.SysParameter;
import cn.agiledata.bank.hibernateBean.SysParameterValue;
import cn.agiledata.bank.myspace.service.AuthenticateService;
import cn.agiledata.bank.myspace.service.IMySpaceService;

import cn.agiledata.bank.scores.form.HisScoresQueryBean;
import cn.agiledata.bank.scores.service.IScoresService;
import cn.agiledata.bank.scores.service.ScoresFactory;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class ScoresAction extends IPBBaseAction implements Constant{
	
	private IScoresService iss = ScoresFactory.getScoresService();
	protected IMySpaceService getMySpaceService() throws IPBException {
		return (IMySpaceService) IPBAppFactory.getObj(IMySpaceService.MGR_ID);
	}
	private AuthenticateService getAuthenticateService(){
        return (AuthenticateService) IPBAppFactory.getObj("authenticateService");
    }
	//积分激活
	public ActionForward activation(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		AccountSummary user = (AccountSummary) WeatherUtil
				.getLoingInfoFromSession(request);

		String scores = iss.activationStatus(new Long(user.getAccountId()).toString());
		Account accountTemp = new Account();
		try {
			accountTemp = getMySpaceService().getAccount(String.valueOf(user.getAccountId()));
		} catch (IPBException e) {
			e.printStackTrace();
		}
		
		boolean ifMobileNotPass = getAuthenticateService().getMobileTelAuthenticationStatus(accountTemp).equals(Constant.AUTHENTICATION_STATUS_UNPASS);
		String ifAccess = "N";
		if(ifMobileNotPass){
			ifAccess = "N";
		}else{
			ifAccess = "Y";
		}
		request.setAttribute("ifMobile", ifAccess);
		
		if (accountTemp.getMobile()!=null && accountTemp.getMobStatus().equals(Constant.ACCOUNT_STATE_UNACTIVATED)) {
			request.setAttribute("mobileText", "激活"+accountTemp.getMobile());
			request.setAttribute("mobileUrl", "/toActivateMob.do");
		} else {
			request.setAttribute("mobileText", "绑定手机并激活");
			request.setAttribute("mobileUrl", "/route.do?modual=space$mobile");
		}
		
		if(StringUtils.isNotEmpty(scores)){
			if ("0".equals(scores)) {
				request.setAttribute(MESSAGE, ACTIVATE_SCORES_ACCOUNT+"0");
			} else {
				request.setAttribute("scores", scores);
				request.setAttribute(MESSAGE, ACTIVATE_SCORES_ACCOUNT);
			}
			return mapping.findForward("success");
		}else{
			Account account = iss.getAccountById(new Long(user.getAccountId()).toString());
			String status = iss.idCardStatus(account);
			if (StringUtils.isNotEmpty(status)) {
				request.setAttribute("status", status);
			}
			return mapping.findForward("failer");
		}
		
	}
	//积分激活状态
	public ActionForward getScores(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse reponse) {
		Account account = iss.getAccountById(String.valueOf
				(WeatherUtil.getAccountIdFromSession(request)));
		request.setAttribute("authentication", iss.getAuthenticateStatus(account));
		request.setAttribute("account", account);
		return mapping.findForward("success");
	}
	//查询积分历史
	public ActionForward getScoresHistory(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws IPBException, Exception{
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		Account account = getMySpaceService().getAccount(String.valueOf(accountId));
		if(account.getAccountType().equals(Constant.ACCOUNT_TYPE_CORPORATION)) {
			return mapping.findForward("globalFail");
		}
			HisScoresQueryBean bean = (HisScoresQueryBean)form;
			bean.setAccountId(String.valueOf(WeatherUtil.getAccountIdFromSession(request)));
			PageBean pb = iss.getHisScoresQueryPagingList(bean);
			request.setAttribute("source", iss.getScoresSource());
			request.setAttribute("scoresList", pb);
		return mapping.findForward("success");
	}

}
